<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const p = new Promise((resolve, reject) => {
        try {
          console.log("请求A的数据......");
          // throw new Error("A错误")
          setTimeout(() => {
            console.log("A的数据请求成功");

            const objA = {
              name: "liuMin"
            }

            resolve(objA)
          }, 3000)
        } catch (e) {
          reject(e.message)
        }
      })


      //如果上面的promise实例成功，则执行then方法，否则直接穿透
      .then((result) => {
        return new Promise((resolve, reject) => {
          try {
            console.log("请求B的数据......");
            // throw new Error("B错误")
            setTimeout(() => {
              console.log("B的数据请求成功");

              const objB = {
                age: 31
              }

              resolve({           //将接收到的返回值与objB对象合并
                ...result,
                ...objB
              })
            }, 3000)
          } catch (e) {
            reject(e.message)
          }
        })
      })
      //如果上面的promise实例成功，则执行then方法，否则直接穿透
      .then((result) => {
        return new Promise((resolve, reject) => {
          try {
            console.log("请求C的数据......");
            // throw new Error("C错误")
            setTimeout(() => {
              console.log("C的数据请求成功");
              const objC = {
                gender: "男"
              }
              resolve({            //将接收到的返回值与objC对象合并
                ...result,
                ...objC
              })
            }, 3000)
          } catch (e) {
            reject(e.message)
          }
        })
      })
      //如果上面promise实例失败，或者任意一个then方法失败，则直接会穿透到catch方法，返回catch值
      .catch(() => {
        return new Promise((resolve, reject) => {
          reject("错误")
        })
      })

    console.log(p);
  </script>
</body>

</html>